/**
 * Created by tianling on 2016/12/25.
 */

module.exports = class {
    constructor() {
        this.key = "cms:site:menu";
    }

    static init() {
        return new this();
    }

    /**
     * 获取系统模块列表
     */
    async getItemsByDB() {
        return await global.DataBase.cms_site_menu.Access.findAll({
            order: ['sm_sort'],
            raw: true
        });
    };

    async Sync() {
        let items = await this.getItemsByDB();
        if (!global.Config.isShareMemory) {
            global.cmsSiteMenu = items;
        } else {
            let redisClient = global.Redis.getClient();
            await redisClient.set(this.key, JSON.stringify(items));
        }
        log.info("站点栏目加载成功");
        return true;
    };


    /**
     * 根据显示位置获取站点菜单
     * @param Label
     * @constructor
     */
    async GetItems(showAddr) {
        let Items = {};
        if (!global.Config.isShareMemory) {
            Items = global.cmsSiteMenu;
        } else {
            let redisClient = global.Redis.getClient();
            let ret = await redisClient.get(this.key);
            Items = JSON.parse(ret);
        }
        if (!isNaN(showAddr)) {
            return Items.filter(it => {
                if (it.sm_showAddr.indexOf(',' + showAddr + ',') > -1) {
                    return true;
                }
                return false;
            });
        }

        return Items;

    };

    /**
     * 获取某个站点菜单
     * @param Label String
     * @param Name String
     * @returns {*}
     * @constructor
     */
    async GetItem(id) {
        let Items = await this.GetItems();
        let _items = Items.filter(function (item) {
            if (item.sm_id === id) {
                return true;
            }
        });
        if (_items.length > 0) {
            return _items[0];
        }
        return null;
    };




    async getChildrenValues(id) {
        let cur = await this.GetItem(id);
        let Items = await this.GetItems();
        let Childrens = [];
        for (let i = 0; i < Items.length; i++) {
            let item = Items[i];
            if (item.sm_parentId === cur.sm_id) {
                Childrens.push(item.sm_id);
                if (item.sm_isParent === 1) {
                    Childrens = Childrens.concat(await this.getChildrenValues(item.sm_id));
                }
            }
        }
        return Childrens;
    };

    async getChildrensModel(id) {
        let cur = await this.GetItem(id);
        let Items = await this.GetItems();
        let Childrens = [];
        for (let i = 0; i < Items.length; i++) {
            let item = Items[i];
            if (item.sm_parentId === cur.sm_id) {
                Childrens.push(item);
                if (item.sm_isParent === 1) {
                    Childrens = Childrens.concat(await this.getChildrenValues(item.sm_id));
                }
            }
        }
        return Childrens;
    };

    /**
     * 获取所有父级的值数组
     * @param {*} Label
     * @param {*} value
     */
    async GetParentsiDs(id) {
        let ParentsModels = await this.GetParentsModel(id);
        let Values = [];
        for (let i = ParentsModels.length - 1; i > -1; i--) {
            Values.push(ParentsModels[i].sm_id);
        }
        return Values;
    };

    /**
     * 获取所有父级的名称数组
     * @param {*} Label
     * @param {*} value
     */
    async GetParentsName(id) {
        let ParentsModels = await this.GetParentsModel(id);
        let Names = [];
        for (let i = ParentsModels.length - 1; i > -1; i--) {
            Names.push(ParentsModels[i].sm_name);
        }
        return Names;
    };

    /**获取所有的父级模型**/
    async GetParentsModel(id) {
        let item = await GetItem(id);
        if (item) {
            return await this.GetParentsModelByID(item.zd_ID);
        }
        return [];
    };

    async GetParentsModelByID(ChildrenID) {
        let Parents = [];
        let item = await this.GetItem(ChildrenID);
        if (item) {
            Parents.push(item);
            Parents = Parents.concat(await this.GetParentsModelByID(item.sm_parentId));
        }
        return Parents;
    };



    async GetTreeData(showAddr, ParentID, _Items) {
        let Items = [];
        if (_Items) {
            Items = _Items;
        } else {
            Items = await this.GetItems(showAddr);
        }
        let data = [];
        let getChildrens = function (pid) {
            let cdata = [];
            for (let i = 0; i < Items.length; i++) {
                let item = Items[i];
                if (item.sm_parentId === pid) {
                    let data_item = {
                        title: item.sm_name,
                        value: item.sm_id,
                        key: item.sm_id,
                        url: item.sm_url,
                        icon: item.sm_icon,
                        showAddr: item.sm_showAddr,
                        attr: item.sm_attr,
                        children: getChildrens(item.sm_id)
                    };
                    if (data_item.children.length === 0) delete data_item.children;
                    cdata.push(data_item);
                }
            }
            return cdata;
        };
        if (ParentID !== '') {
            let item = await this.GetItem(ParentID);
            data.push({
                title: item.sm_name,
                value: item.sm_id,
                key: item.sm_id,
                url: item.sm_url,
                icon: item.sm_icon,
                showAddr: item.sm_showAddr,
                attr: item.sm_attr,
                children: getChildrens(item.sm_id)
            });
        } else {
            data = getChildrens("");
        }
        return data;

    };


};